<?php
/**
 * 资源服务
 *
 * User: Jay
 * Date: 2018/8/8
 */

namespace ServiceBundle\Controller;


use BaseBundle\Controller\BaseController;
use Endroid\QrCode\QrCode;
use Gregwar\Captcha\CaptchaBuilder;
use ServiceBundle\Services\Update;
use Symfony\Component\HttpFoundation\Response;
use Ramsey\Uuid\Uuid;

class ResourceController extends BaseController
{
    /**
     * 生成二维码控制器
     *
     * @return Response
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
     * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
     * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
     */
    public function qrCodeAction()
    {
        header('Content-type: image/jpeg');
        header('Pragma: no-cache');
        header('Cache-Control: no-cache');

        $siteUrl = urldecode(self::$request->query->get('site_uri', 'not uri'));
        $qrCode = new QrCode();
        $qrCode
            ->setText($siteUrl)
            ->setSize(300)
            ->setPadding(10)
            ->setErrorCorrection('high')
            ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
            ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
            ->setLabelFontSize(16)
            ->setImageType(QrCode::IMAGE_TYPE_PNG)
        ;

        header('Content-Type: '.$qrCode->getContentType());
        header('Pragma: no-cache');
        header('Cache-Control: no-cache');

        $qrCode->render();

        return new Response();
    }


    /**
     * 图形验证码控制器
     *
     * @return Response
     */
    public function captchaAction()
    {
        $type = self::$request->query->get('type', 'captcha');

        $builder = new CaptchaBuilder();
        $builder
            ->setIgnoreAllEffects(true)
            ->build();

        $this->get('unicorn.sessions')->set($type, $builder->getPhrase());

        if($this->container->hasParameter('platform_android')){
            return new Response($builder->inline());  // 返回base64图形码
        }

        header('Content-type: image/jpeg');
        header('Pragma: no-cache');
        header('Cache-Control: no-cache');

        $builder->output();

        return new Response();
    }


    /**
     * 上传文件资源控制器
     *
     * @return array|Response
     */
    public function uploadAction()
    {
        $update = new Update($this->container);

        $image_is_save_local = true;

        if($this->container->hasParameter('resource_is_save_local')){
            $image_is_save_local = $this->getParameter('resource_is_save_local');
        }


        $data = json_encode($update->upload(
            self::$request->get('file_type', 0),
            null,
            self::$request->get('input_name', 'request'),
          '', $image_is_save_local));

        return new Response($data);
    }

    /**
     * 获取唯一值
     *
     * @return array|bool|Response
     * @throws \Exception
     */
    public function generateUuidAction()
    {
        $data['uuid'] = Uuid::uuid1()->toString();

        return $this->response('成功得到数据',0, $data);
    }
}